package com.example.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shopping.common.consts.MessageEnum;
import com.example.shopping.dao.UserDao;
import com.example.shopping.pojo.domain.UserBean;
import com.example.shopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author Dream
 * @version 1.0
 * @date 2020/11/9 12:02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao,UserBean> implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 登录检查
     * @param xh
     * @param password
     * @return
     */
    @Override
    public HashMap<String,Object> getUser(String xh,String password,HttpServletRequest request) {
        UserBean userBean=userDao.getUser(xh);
        HashMap<String,Object> map=new HashMap<String, Object>();
        if(userBean==null){
            map.put("user",MessageEnum.XH_NO_EXIST);
        }else if(!password.equals(userBean.getPassword())){
            map.put("user",MessageEnum.PASSWORD_ERROR);
        }else{
            //禁止返回密码
            userBean.setPassword("");
            //存入缓存
            request.getSession().setAttribute("user",userBean);
            map.put("user",userBean);
        }
        return map;
    }

    /**
     * 添加新会员
     * @param userBean
     * @return
     */
    @Override
    public String addUser(UserBean userBean) {
        UserBean user=userDao.getUser(userBean.getXh());
        if(user==null){
            //添加用户
            userDao.addUser(userBean);
            return MessageEnum.USER_ADD_SUCCESS;
        }else{
            return MessageEnum.XH_EXIST;
        }
    }

    @Override
    public String updatePswd(String password, String xh) {
        UserBean userBean=userDao.getUser(xh);
        if(!password.equals(userBean.getPassword())){
            userDao.updatePswd(password,xh);
            return MessageEnum.PASSWORD_UPDATE_SUCCESS;
        }else{
            return MessageEnum.PASSWORD_IDENTICAL;
        }
    }

    /**
     * 修改密码
     * @param password
     * @param xh
     * @return
     */

}
